package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.LineStandInfo;
import cn.com.yx.ms.domain.line.DevStandModel;
import cn.com.yx.ms.service.IStandService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "stand")
@Api(value = "站台线路设备接口", description = "用于站台线路数据资源获取和操作的接口")
public class StandController {

    private static final Logger logger = LoggerFactory.getLogger(StandController.class);

    @Resource
    private IStandService iStandService;

    // region << 设备站台关联 >>
    @PostMapping("")
    @ApiOperation(value = "保存设备和站台实体", notes = "保存设备和站台实体")
    @ApiImplicitParam(paramType = "body", name = "devStandModel", dataType = "Map", required = true, value = "保存设备和站台实体")
    public ApiResult save(@RequestBody @Validated DevStandModel devStandModel, BindingResult bindingResult) {
        return iStandService.save(devStandModel);
    }

    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的设备站台数据")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("devsn", request.getParameter("devsn"));
        query.getConditions().put("sname", request.getParameter("sname"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<DevStandModel> page = (Page<DevStandModel>) iStandService.find(query);
        ApiPager<DevStandModel> apiPager = new ApiPager<>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除设备站台关联", notes = "删除设备站台")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "设备equipmentId")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iStandService.delete(id));
    }

    @ResponseBody
    @RequestMapping(value = "{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "设备ID")
    @ApiOperation("获取单个设备站台绑定信息")
    public ApiResult findById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iStandService.findOne(id));
    }
    // endregion

    // region << 站台管理 >>

    @ResponseBody
    @RequestMapping(value = "standinfos", method = RequestMethod.GET)
    @ApiOperation("获取所有设备信息")
    public ApiResult findStandInfos(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("sname", request.getParameter("sname"));
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iStandService.findAllStandInfo(query));
    }

    @ResponseBody
    @RequestMapping(value = "standinfos/page", method = RequestMethod.GET)
    @ApiOperation("获取所有设备信息")
    public ApiResult findStandInfosPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("sname", request.getParameter("sname"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<LineStandInfo> page = (Page<LineStandInfo>) iStandService.findAllStandInfo(query);
        ApiPager<LineStandInfo> apiPager = new ApiPager<>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);



    }

    // endregion
    @ResponseBody
    @RequestMapping(value = "line/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的线路数据")
    public ApiResult findLineByconditions(@ApiQuery ApiQueryBase query, HttpServletRequest request){
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("lname", request.getParameter("lname"));
        query.getConditions().put("sname", request.getParameter("sname"));
        return iStandService.findLineinfor(query);

    }
}